export function device() {
  var MobileDetect = require('mobile-detect');
  const ua = navigator.userAgent;
  var md = new MobileDetect(ua);
  return {
    systemVersion: getDeviceVersion(),
    mobileType: md.os(),
    mobileModel: getDeviceModel(md),
  };
}
/**
 * 获取设备型号
 */
function getDeviceModel(md) {
  var device_type = navigator.userAgent; //获取userAgent信息
  var os = md.os(); //获取系统
  var model = '';
  if (os == 'iOS') {
    //ios系统的处理
    model = md.mobile();
  } else if (os == 'AndroidOS') {
    //Android系统的处理
    var sss = device_type.split(';');
    var i = sss.findIndex((item) => item.includes('Build/'));
    if (i > -1) {
      model = sss[i].substring(0, sss[i].indexOf('Build/'));
    }
  }
  return model;
}
/**
 * 获取设备系统版本号
 */
function getDeviceVersion() {
  var u = navigator.userAgent,
    version = '';
  if (u.indexOf('Mac OS X') > -1) {
    //ios
    var regStr_saf = /OS [\d._]*/gi;
    var verinfo = u.match(regStr_saf);
    version = (verinfo + '').replace(/[^0-9|_.]/gi, '').replace(/_/gi, '.');
  } else if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
    //android
    version = u.substr(u.indexOf('Android') + 8, u.indexOf(';', u.indexOf('Android')) - u.indexOf('Android') - 8);
  } else if (u.indexOf('BB10') > -1) {
    //黑莓bb10系统
    version = u.substr(u.indexOf('BB10') + 5, u.indexOf(';', u.indexOf('BB10')) - u.indexOf('BB10') - 5);
  } else if (u.indexOf('IEMobile')) {
    //windows phone
    version = u.substr(u.indexOf('IEMobile') + 9, u.indexOf(';', u.indexOf('IEMobile')) - u.indexOf('IEMobile') - 9);
  }
  return version;
}
